<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="岗位名称" prop="workName">
        <el-input v-model="formData.workName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="招聘人数" prop="recruitsNum">
        <el-input v-model="formData.recruitsNum" placeholder="请输入招聘人数" />
      </el-form-item>
      <el-form-item label="职位详情" prop="workDetail">
        <el-input v-model="formData.workDetail" placeholder="请输入职位详情" />
      </el-form-item>
      <el-form-item label="工作内容" prop="workContent">
        <Editor v-model="formData.workContent" height="150px" />
      </el-form-item>
      <el-form-item label="工作地址" prop="workAdress">
        <el-input v-model="formData.workAdress" placeholder="请输入工作地址" />
      </el-form-item>
      <el-form-item label="工作环境照片" prop="workPhoto">
        <el-input v-model="formData.workPhoto" placeholder="请输入工作环境照片" />
      </el-form-item>
      <el-form-item label="对应公司" prop="companyId">
<!--        <el-input v-model="formData.companyId" placeholder="请输入对应公司id" />-->
        <el-select
          v-model="formData.companyId"
          clearable
          placeholder="请选择"
        >


          <el-option
            v-for="item in companyData"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="上班时间" prop="workTime">
        <el-date-picker
          v-model="formData.workTime"
          type="date"
          value-format="x"
          placeholder="选择上班时间"
        />
      </el-form-item>
      <el-form-item label="休息时间" prop="workRest">
        <el-input v-model="formData.workRest" placeholder="请输入休息时间" />
      </el-form-item>
      <el-form-item label="工资" prop="workMoney">
        <el-input v-model="formData.workMoney" placeholder="请输入工资" />
      </el-form-item>
      <el-form-item label="工作福利" prop="workWelfare">
        <el-input v-model="formData.workWelfare" placeholder="请输入工作福利" />
      </el-form-item>
      <el-form-item label="学历" prop="degree">
        <el-input v-model="formData.degree" placeholder="请输入学历" />
      </el-form-item>
      <el-form-item label="工种" prop="jobs">
        <el-input v-model="formData.jobs" placeholder="请输入工种" />
      </el-form-item>
      <el-form-item label="结算方式" prop="settlement">
        <el-input v-model="formData.settlement" placeholder="请输入结算方式" />
      </el-form-item>
      <el-form-item label="年龄要求" prop="age">
        <el-input v-model="formData.age" placeholder="请输入年龄要求" />
      </el-form-item>
      <el-form-item label="是否面议" prop="negotiable">
        <el-input v-model="formData.negotiable" placeholder="请输入是否面议" />
      </el-form-item>
      <el-form-item label="其他要求" prop="msg">
        <el-input v-model="formData.msg" placeholder="请输入其他要求" />
      </el-form-item>
      <el-form-item label="岗位类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择岗位类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否关闭" prop="open">
        <el-input v-model="formData.open" placeholder="请输入是否关闭" />
      </el-form-item>
      <el-form-item label="是否外出岗位" prop="wordOut">
        <el-input v-model="formData.wordOut" placeholder="请输入是否外出岗位" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="审核通过" prop="state">
        <el-input v-model="formData.state" placeholder="请输入审核通过" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { JobsInfoApi, JobsInfoVO } from '@/api/crm/jobsinfo'
import {CompanyInfoApi} from "@/api/crm/companyinfo";

/** 招工岗位 表单 */
defineOptions({ name: 'JobsInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const companyData = ref([]);
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  workName: undefined,
  recruitsNum: undefined,
  workDetail: undefined,
  workContent: undefined,
  workAdress: undefined,
  workPhoto: undefined,
  companyId: undefined,
  companyName:undefined,
  workTime: undefined,
  workRest: undefined,
  workMoney: undefined,
  workWelfare: undefined,
  degree: undefined,
  jobs: undefined,
  settlement: undefined,
  age: undefined,
  negotiable: undefined,
  msg: undefined,
  type: undefined,
  open: undefined,
  wordOut: undefined,
  sort: undefined,
  state: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  //获取公司信息
  let res  =await CompanyInfoApi.getCompanyInfoPage({ pageSize:100 })
  console.log(res.list)
    companyData.value=res.list
  console.log(companyData.value)


  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await JobsInfoApi.getJobsInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as JobsInfoVO
    if (formType.value === 'create') {
      await JobsInfoApi.createJobsInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await JobsInfoApi.updateJobsInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    workName: undefined,
    recruitsNum: undefined,
    workDetail: undefined,
    workContent: undefined,
    workAdress: undefined,
    workPhoto: undefined,
    companyId: undefined,
    workTime: undefined,
    workRest: undefined,
    workMoney: undefined,
    workWelfare: undefined,
    degree: undefined,
    jobs: undefined,
    settlement: undefined,
    age: undefined,
    negotiable: undefined,
    msg: undefined,
    type: undefined,
    open: undefined,
    wordOut: undefined,
    sort: undefined,
    state: undefined,
  }
  formRef.value?.resetFields()
}
</script>
